package com.spring.api.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.spera.genutil.EzvizUtils;
import com.spring.api.web.dao.EzvizMapper;
import com.spring.api.web.dao.VideoMonitorOpenMapper;
import com.spring.api.web.model.VideoMonitorOpen;
import com.spring.api.web.service.EnvizApiService;
import com.spring.common.web.dao.STracelogboMapper;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.TracelogboMapper;
import com.sso.api.web.model.TracelogboWithBLOBs;

@Service
public class EnvizApiServiceImpl implements EnvizApiService{
	
	@Resource private EzvizMapper ezvizMapper;
	
	@Resource private VideoMonitorOpenMapper videoMonitorOpenMapper;

	@Resource private JedisCache jedisCache;
	
	@Resource private STracelogboMapper sTracelogboMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public String accessToken(Map<String,Object> parm) {
		int isIn = ezvizMapper.checkEzviz(parm);
		if(isIn==0){
			return null;
		}
		String retAcc = "";
		Map<String, Object> paramsMap = Maps.newHashMap();
		paramsMap.put("phone", "17131622587");
		Map<String, Object> ret = EzvizUtils.paramsInit("token/getAccessToken", paramsMap);
		Map<String, Object> accessToken = EzvizUtils.doPost(ret);
		Map<String, Object> retM = (Map<String, Object>)accessToken.get("result");
		if("200".equals(ApplicationUtils.Object2String(retM.get("code")))){
			retM = (Map<String, Object>)accessToken.get("result");
			Map<String, Object> data = (Map<String, Object>)retM.get("data");
			retAcc = ApplicationUtils.Object2String(data.get("accessToken"));
		}
		
		return retAcc;
	}

	@Override
	public Map<String, Object> ezvizList(String accessToken) {
		Map<String, Object> paramsMap = Maps.newHashMap();
		paramsMap.put("accessToken", accessToken);
		paramsMap.put("pageSize", 999);
		paramsMap.put("pageStart", 0);
		Map<String, Object> ret = EzvizUtils.paramsInit("cameraList", paramsMap);
		
		Map<String, Object> retM = EzvizUtils.doPost(ret);
		return retM;
	}
	
	@Override
	public int getRemainTime(){
		int remainTime=0;
		int isSpecial = ezvizMapper.isSpecial();
		if(isSpecial==1){
			remainTime=ezvizMapper.remainSpecialTime();
		}else{
			remainTime=ezvizMapper.remainTime();
		}
		return remainTime;
	}
	
	@Override
	public List<Map<String, Object>> ezvizs(Map<String, Object> parm) {
		int remainTime=getRemainTime();
		if(remainTime<=0){
			return new ArrayList<Map<String, Object>>();
		}
		int isIn = ezvizMapper.checkEzviz(parm);
		if(isIn==0){
			return new ArrayList<Map<String, Object>>();
		}
		//获取本地摄像头配置
		List<Map<String, Object>> retLocalData = ezvizMapper.queryEzviz(parm);
		//获取萤石摄像头列表
		List<Map<String, Object>> dataList = getEzvizDataist();
		//最终数据
		List<Map<String, Object>> dataSet = Lists.newArrayList();
		if(dataList!=null){
			Map<String, Object> dataOne = Maps.newHashMap();
			for(int i=0;i<dataList.size();i++){
				dataOne = dataList.get(i);
				
				//如果发现本地列表中没有改设备 则移除萤石云设备信息
				boolean temp=false;
				for(Map<String, Object> loDataOne:retLocalData){

					if(dataOne.get("cameraId").equals(loDataOne.get("camera_id"))){
						temp=true;
						dataList.get(i).put("ezvizId", loDataOne.get("id"));
						dataList.get(i).put("kidsPark", ApplicationUtils.Object2String(loDataOne.get("kidsPark")));
						dataList.get(i).put("kidsParkName", ApplicationUtils.Object2String(loDataOne.get("kidsParkName")));
						dataList.get(i).put("ezvizType", ApplicationUtils.Object2String(loDataOne.get("ezvizType")));
						dataList.get(i).put("ezvizTypeName", ApplicationUtils.Object2String(loDataOne.get("ezvizTypeName")));
						dataList.get(i).put("ezvizName", ApplicationUtils.Object2String(loDataOne.get("ezvizName")));
						dataList.get(i).put("orderProductId", ApplicationUtils.Object2String(loDataOne.get("orderProductId")));
						dataList.get(i).put("productName", ApplicationUtils.Object2String(loDataOne.get("productName")));
						dataList.get(i).put("inStatus", ApplicationUtils.Object2String(loDataOne.get("inStatus")));
//						dataList.get(i).put("picUrl", ApplicationUtils.Object2String(loDataOne.get("ezviz_pic")));
						dataList.get(i).put("cameraName", ApplicationUtils.Object2String(loDataOne.get("ezvizName")));
						dataList.get(i).put("remaintime",remainTime);
						dataSet.add(dataList.get(i));
					}
				}
				if(temp==false){
					dataList.remove(i);
					i--;
				}
				
			}
		}
		
		return dataSet;
	}
	
	//根据萤石云接口获取设备列表数据
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getEzvizDataist(){
		Map<String, Object> accessToken = accessTokenLo();
		Map<String, Object> ret = (Map<String, Object>)accessToken.get("result");
		if("200".equals(ApplicationUtils.Object2String(ret.get("code")))){
			ret = (Map<String, Object>)accessToken.get("result");
			Map<String, Object> data = (Map<String, Object>)ret.get("data");
			String accesstk = ApplicationUtils.Object2String(data.get("accessToken"));
			if(StringUtils.isNotBlank(accesstk)){
				Map<String, Object> retData = ezvizList(accesstk);
				ret = (Map<String, Object>)retData.get("result");
				List<Map<String, Object>> dataList = (List<Map<String, Object>>)ret.get("data");
				for(int i=0;i<dataList.size();i++){
					dataList.get(i).put("kidsPark","");
					dataList.get(i).put("kidsParkName", "");
					dataList.get(i).put("ezvizType", "");
					dataList.get(i).put("ezvizTypeName", "");
					dataList.get(i).put("orderProductId", "");
					dataList.get(i).put("productName", "");
					dataList.get(i).put("inStatus", "");
				}
				return dataList;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	private Map<String, Object> accessTokenLo() {
		Map<String, Object> paramsMap = Maps.newHashMap();
		paramsMap.put("phone", "17131622587");
		Map<String, Object> ret = EzvizUtils.paramsInit("token/getAccessToken", paramsMap);
		
		Map<String, Object> accessToken = EzvizUtils.doPost(ret);
		return accessToken;
	}

	@Override
	public List<VideoMonitorOpen> checkVideoList(VideoMonitorOpen parm) {
		return videoMonitorOpenMapper.selectByParm(parm);
	}
	
	public void insertLog(String id,String token,HttpServletRequest request){
		TracelogboWithBLOBs tl = MyTools.getLog(jedisCache, token, request);
		tl.setEntitytypename("base_ezviz");
		tl.setEntityid(id);
		tl.setLogcategory(99);
		tl.setLogdescription(null);
		int effect=sTracelogboMapper.insert(tl);
		System.out.println(effect);
	}

}
